import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
//批量插入
public class PutDemo2 {
    static Configuration conf = null;

    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.rootdir", "hdfs://192.168.126.139:9000/hbase");
        conf.set("hbase.master", "hdfs://192.168.126.139:60010");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("hbase.zookeeper.quorum", "192.168.126.139");
    }

    public static void putList(String tableName, String[] rowKeys, String[] families, String[] columns, String[] values) {
        try {
            HBaseAdmin admin = new HBaseAdmin(conf);
            HTable table = new HTable(conf, tableName.valueOf(tableName));
            int length = rowKeys.length;
            List<Put> putList = new ArrayList<>();
            if (!admin.tableExists(Bytes.toBytes(tableName))) {
                System.err.println("the" + tableName + "is no exist");
                System.exit(1);
            }
            for (int i = 0; i < length; i++) {
                Put put = new Put(Bytes.toBytes(rowKeys[i]));
                put.add(Bytes.toBytes(families[i]), Bytes.toBytes(columns[i]), Bytes.toBytes(values[i]));
                putList.add(put);
            }
            table.put(putList);
            table.close();
            System.out.println("插入成功");
        } catch (Exception e) {
        }
    }

    public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
        putList("stu", new String[]{"rw002", "rw002"}, new String[]{"info", "grade"}, new String[]{"name", "Java"}, new String[]{"60", "70"});
    }
}

